﻿using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Drawing;
using Microsoft.DirectX.Direct3D;

namespace NGE
{
    [Serializable]
    public class NImage: NFile
    {
        public NImage()
        {
            CreateTime = DateTime.Now;
            Name = "";
            GUID = Guid.NewGuid();
            Version = "0";
        }
        public NImage(string fileName)
            : this()
        {
            GetData(fileName);
        }

        public List<byte> Data;

        [NonSerialized]
        protected byte[] data;
        public byte[] DataByte
        {
            get
            {
                if (data == null)
                {
                    data = new byte[Data.Count];
                    int i = 0;
                    foreach (byte b in Data)
                    {
                        data[i] = b;
                        i++;
                    }
                    //Data.Clear();
                }
                return data;
            }
        }
        public MemoryStream DataStream
        {
            get
            {
                return new MemoryStream(DataByte);
            }
        }
        [NonSerialized]
        Image memoryImage;
        [NonSerialized]
        Texture memoryTexture;

        public Image MemImage
        {
            get
            {
                return memoryImage;
            }
        }
        public Texture MemTexture
        {
            get
            {
                return memoryTexture;
            }
        }
                
        public Size Size;

        public int Width
        {
            get
            {
                return Size.Width;
            }
        }
        public int Height
        {
            get
            {
                return Size.Height;
            }
        }


        public void Init()
        {
            memoryImage = Image.FromStream(DataStream);
        }
        public void Init(Device d3ddev)
        {
            memoryImage = Image.FromStream(DataStream);
            memoryTexture = new Texture(d3ddev, memoryImage.Width, memoryImage.Height, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
            try
            {
                using (Graphics g = memoryTexture.GetSurfaceLevel(0).GetGraphics())
                {
                    //g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
                    //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                    g.DrawImage(memoryImage, new Point());
                }
            }
            catch { }
        }
        new public void Dispose()
        {
            data = null;
            if (memoryTexture != null)
            {
                memoryTexture.Dispose();
            }        
            if (memoryImage != null)
            {
                memoryImage.Dispose();
            }
        }
        //public void GetData(byte[] d,Size s)
        //{
        //    data = d;
        //    Size = s;

        //    Data = new List<byte>();
        //    foreach (byte b in data)
        //    {
        //        Data.Add(b);
        //    }
        //}
        //public void GetData(string fileName, Size s)
        //{
        //    data = File.ReadAllBytes(fileName);
        //    Size = s;

        //    Data = new List<byte>();
        //    foreach (byte b in data)
        //    {
        //        Data.Add(b);
        //    }
        //}
        public void GetData(string filename)
        {
            FileIndexer fi = new FileIndexer(filename);
            if (fi.ExtName.ToLower() == ".png")
            {
                GetData( Image.FromFile(filename));
            }
            else if (fi.ExtName.ToLower() == ".nimg")
            {
                GetData( (NImage)Util.LoadObject(filename));
            }
        }
        public void GetData( NImage nimg)
        {
            GetData( nimg.DataByte);
        }
        public void GetData( byte[] raw)
        {
            GetData( Image.FromStream(new MemoryStream(raw)));
        }
        public void GetData( Image img)
        {
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            data = ms.ToArray();
            Size = img.Size;

            Data = new List<byte>();
            foreach (byte b in data)
            {
                Data.Add(b);
            }
        }
    }
}
